﻿namespace DCHealthCheck.MvcWeb.Classes {

    #region Usings
    using DCHealthCheck.DomainModel;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Principal;
    using System.Threading;
    using System.Web;
    #endregion

    /// <summary>
    /// Creates new instances of IEntityManager objects, passing in the current HttpContext Identity, if available
    /// </summary>
    /// <remarks>IOC LifeStyle should be per web request.  Disposed of by IOC.</remarks>
    public class EntityManagerFactory : IEntityManagerFactory {

        public EntityManagerFactory() {
        }

        public IEntityManager GetInstance() {
            var identity = (HttpContext.Current != null) && (HttpContext.Current.User != null)
                && (HttpContext.Current.User.Identity != null) && (HttpContext.Current.User.Identity.IsAuthenticated)
                ? HttpContext.Current.User.Identity
                : WindowsIdentity.GetCurrent();
            var entityManager = new EntityManager(identity);
            return entityManager;
        }
    }
}